package com.right.health.app.dialog;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.magic.basic.common.SendAuthCodeCommon;
import com.right.health.app.R;

/**
 * @author: weming
 * @date: 2021/5/7
 * @description:服务包支付验证码输入
 */
public class ServiceCodeInputDialog extends BaseDialog {

    private EditText mEditText;
    private TextView mTimeTv;
    private SendAuthCodeCommon mSendAuthCodeCommon;
    private boolean mIsTiming;

    public ServiceCodeInputDialog(Context context, String phone, View.OnClickListener clickListener) {
        super(context);
        mEditText = mRootView.findViewById(R.id.et_code);
        mTimeTv = mRootView.findViewById(R.id.tv_get_verification);
        mTimeTv.setOnClickListener(clickListener);
        TextView tipTv = findViewById(R.id.tv_tips);
        String showNum = "";
        try {
            showNum = phone.substring(0, 3) + "****" + phone.substring(7);
        } catch (Exception e) {
            e.printStackTrace();
        }
        tipTv.setText("验证码已发送至" + showNum);
        mSendAuthCodeCommon = new SendAuthCodeCommon(milliseconds -> {
            mTimeTv.setText(milliseconds == 0 ? getContext().getString(R.string.again_send_code) :
                    String.format(getContext().getString(R.string.format_seconds),
                            String.valueOf(milliseconds / 1000)));

            if (milliseconds == 0) {
                mTimeTv.setClickable(true);
                mIsTiming = false;
            }
        });
    }

    @Override
    protected View getRootView(Context context) {
        return LayoutInflater.from(context).inflate(R.layout.dialog_service_code_input, null);
    }

    public void startTiming() {
        if (!mIsTiming) {
            mTimeTv.setClickable(false);
            mIsTiming=true;
            mSendAuthCodeCommon.startCountdown();
        }
    }

    public String getInput() {
        return mEditText.getText().toString();
    }


}
